package com.rock.upms.client.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rock.common.base.BaseController;
import com.rock.common.base.BaseResult;
import com.rock.common.util.StringUtil;
import com.rock.upms.api.model.service.IUpmsSystemService;
import com.rock.upms.api.model.service.IUpmsUserService;
import com.rock.upms.api.upms.service.IUpmsService;
import com.rock.upms.client.config.Global;
import com.rock.upms.client.shiro.session.UpmsSessionDao;
import com.rock.upms.client.util.MenuUtil;
import com.rock.upms.client.util.UserUtil;
import com.rock.upms.model.dto.RockMenu;
import com.rock.upms.model.dto.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 单点登录管理 Created by hotdog on 2018/1/10.
 */
@Controller
@Api(value = "登录验证", description = "登录验证")
public class IndexController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(IndexController.class);
	@Autowired
	IUpmsSystemService upmsSystemService;
	@Autowired
	IUpmsUserService upmsUserService;
	@Autowired
	UpmsSessionDao upmsSessionDao;
	@Autowired
	private IUpmsService upmsService;

	@ApiOperation(value = "index管理主页初始化")
	@RequestMapping(value = "${adminPath}/index")
	public String index(HttpServletRequest request, Model model) {
		log.trace("--->进入index页面初始化，请求系统："+Global.getSysName());
		UserDto user = UserUtil.getUser();
		if (user == null) {
			log.info("--->获取userDto失败，sso登录页面");
			return "public/core/rockLogin";
		}
		// 登录成功后进入菜单导航首页
		log.trace("---> 获取top菜单begin:" + user.getUsercode());
		List<RockMenu> topMenuList = upmsService.getSystemMenuAll(UserUtil.getUser().getId());
		log.trace("---> 获取top菜单end:" + user.getUsercode() + "top菜单size:" + topMenuList.size());
		model.addAttribute("topMenuList", topMenuList);
		return "public/core/rockIndex";
	}

	// 获取系统ID对应的菜单列表
	@ResponseBody
	@RequestMapping(value = "${adminPath}/index/sysmenu/{id}", method = RequestMethod.POST)
	public BaseResult list(@PathVariable("id") String id) {
		Integer ouUserId = UserUtil.getUser().getId();
		if (ouUserId == null || ouUserId == 0) {
			log.info("--->获取userDto失败，返回重新登录提示");
			return BaseResult.fail("请重新登录");
		} else {
			List<RockMenu> menuList = upmsService.getMenuAll(ouUserId, StringUtil.toInteger(id));
			for (RockMenu rockMenu : menuList) {
				log.trace("--->left菜单：" + rockMenu.toString());
			}
			log.trace("--->left菜单转html：" + MenuUtil.toHtml(menuList));
			return BaseResult.success("").add("menuList", menuList).add("menuHtml", MenuUtil.toHtml(menuList));
		}
	}
}